/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monographsinpdf;

import cz.mzk.kramerius.imports.Datastream;
import cz.mzk.kramerius.imports.DatastreamFactory;
import cz.mzk.kramerius.imports.FoxmlHelper;
import cz.mzk.kramerius.imports.Monograph;
import cz.mzk.kramerius.imports.Policy;
import cz.mzk.rehan.commons.DatastreamId;
import cz.mzk.rehan.commons.Dom4jUtils;
import cz.mzk.rehan.commons.Model;
import java.io.File;
import java.io.FileNotFoundException;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class MonographInPdfBuilder {

    private final Document k3monograph2ModsXslt;
    private final Document k3monograph2DcXslt;
    private final File conversionDir;

    /**
     *
     * @throws FileNotFoundException if some xsl template file cannot be found
     * @throws DocumentException in case of error in loading xsl template
     */
    public MonographInPdfBuilder(File k3monograph2ModsXslt, File k3monograph2DcXslt, File conversionDir) throws FileNotFoundException, DocumentException {
        this.k3monograph2ModsXslt = Dom4jUtils.loadDocument(k3monograph2ModsXslt, true);
        this.k3monograph2DcXslt = Dom4jUtils.loadDocument(k3monograph2DcXslt, true);
        this.conversionDir = conversionDir;
    }

    public String createAndSaveMonograph(File k3monograph, File pdf) throws Exception {
        Monograph monograph = createMonograph(k3monograph, pdf);
        FoxmlHelper.saveFoxml(conversionDir, monograph);
        return monograph.getPid();
    }

    private Monograph createMonograph(File k3monograph, File pdf) throws FileNotFoundException, DocumentException {
        Document k3monographDoc = Dom4jUtils.loadDocument(k3monograph, true);
        Monograph monograph = createMonographSkeleton(k3monographDoc);
        Datastream mods = createModsDatastream(k3monographDoc, monograph);
        monograph.addDatastream(mods);
        Datastream dc = createDcDatastream(k3monographDoc, monograph);
        monograph.addDatastream(dc);
        Datastream policy = FoxmlHelper.createPolicyDatastream(Policy.PUBLIC);
        monograph.addDatastream(policy);
        Datastream relsExt = createRelsExtDatastream(monograph);
        monograph.addDatastream(relsExt);
        Datastream imgFull = createImgFullDatastream(pdf);
        monograph.addDatastream(imgFull);
        Datastream imgThumb = createImgThumbDatastream(pdf);
        monograph.addDatastream(imgThumb);
        //TODO: mozna jeste IMG_PREVIW
        return monograph;
    }

    private Monograph createMonographSkeleton(Document k3monographDoc) {
        String uuid = FoxmlHelper.generateUuid();
        String label = findLabel(k3monographDoc);
        String signature = findSignature(k3monographDoc);
        String sysno = findSysno(k3monographDoc);
        Monograph monograph = new Monograph(uuid, label);
        monograph.setSignature(signature);
        monograph.setSysno(sysno);
        return monograph;
    }

    private Datastream createModsDatastream(Document k3monographDoc, Monograph monograph) {
        Document modsDoc = Dom4jUtils.transformDocument(k3monographDoc, k3monograph2ModsXslt);
        //todo: updateMods() - pridat uuid apod
        FoxmlHelper.removeSchema(modsDoc);
        return DatastreamFactory.createInlineDatastream(modsDoc, DatastreamId.BIBLIO_MODS);
    }

    private Datastream createDcDatastream(Document k3monographDoc, Monograph monograph) throws DocumentException {
//        Document dcDoc = Dom4jUtils.transformDocument(k3monographDoc, k3monograph2DcXslt);
//        //todo: updateDc() - pridat uuid apod
//        FoxmlHelper.removeSchema(dcDoc);
        Document dcDoc = Dom4jUtils.loadDocument("/home/rehan/knizky_mlp/test_dc.xml", false);
        return DatastreamFactory.createInlineDatastream(dcDoc, DatastreamId.DC);
    }

    private Datastream createRelsExtDatastream(Monograph monograph) {
        Document relsExtDoc = FoxmlHelper.createRelsExtSkeleton(monograph, Model.MONOGRAPH.toString(), Policy.PUBLIC);
        return DatastreamFactory.createInlineDatastream(relsExtDoc, DatastreamId.RELS_EXT);
    }

    private String findLabel(Document k3monographDoc) {
        return "TODO:label";
    }

    private String findSignature(Document k3monographDoc) {
        return "TODO:signature";
    }

    private String findSysno(Document k3monographDoc) {
        return "TODO:sysno";
    }

    private Datastream createImgFullDatastream(File pdf) {
        //todo: bude obsahovat dany soubor a bude bud externally referenced anebo radeji
        //managed. Url pokud bude na soubor, tak ten musi byt dostupne Fedore, ktera to sklizi
        String url = "file:" + pdf.getAbsolutePath();
        return DatastreamFactory.createReferencedManagedDs(url, DatastreamId.IMG_FULL, "application/pdf");
    }

    private Datastream createImgThumbDatastream(File pdf) {
        //todo: bude se muset vyrobit nahled prvni strany pdf.
        //tj. soubor musi byt dostupny i na stroji, kde se foxml vytvari.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
